package com.sibbay.vta.tools;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import com.sibbay.tools.Parser;
import com.sibbay.vta.cache.Net;

import android.test.AndroidTestCase;

public class ParserTest extends AndroidTestCase {

	private static final String TEST_TDL = 
		"Route 901 - Northbnd - Weekday\n" +
		"TRSA STN 	Santa Teresa Light Rail Sta.\n" +   
		"SNEL STN 	Snell Light Rail Station\n" +        
		"OHLN CHYN	Ohlone-Chynoweth Light Rail Sta.\n" +
		"CAPT STN 	Capitol Light Rail Station\n" +      
		"TAMN STN 	Tamien Light Rail Station\n" +       
		"CONV CTR 	San Jose Convention Center\n" +      
		"SANT CLRA	Santa Clara Light Rail Station\n" +  
		"LVE  CLRA	Leave Santa Clara\n" +
		"CIVC CTR 	Civic Center Light Rail Station\n" + 
		"MTRO STA.	Metro Light Rail Station\n" +        
		"TASM STA.	Tasman Station\n" +
		"LVE  TASM	Leave Tasman\n" + 
		"BAY  PT. 	Baypointe Light Rail Station\n" +    
		"GRT. MALL	Great Mall Transit Center\n" +
		"HOST STA.	Hostetter\n" +
		"PENT T.C.	Penitencia Creek Transit Ctr\n" +
		"ALUM ROCK	Alum Rock\n" +
		"\n" +
		"TRSA STN 	SNEL STN 	OHLN CHYN	CAPT STN 	TAMN STN 	CONV CTR 	SANT CLRA	LVE  CLRA	CIVC CTR 	MTRO STA.	TASM STA.	LVE  TASM	BAY  PT. 	GRT. MALL	HOST STA.	PENT T.C.	ALUM ROCK 	\n" +
		"4:16A	4:21A	4:25A	4:28A	4:33A	4:39A	4:44A		4:51A	4:56A	5:05A			5:14A	5:20A	5:24A	5:30A	\n" +
		"	4:56A	5:00A	5:03A	5:08A	5:14A	5:19A		5:26A	5:31A	5:40A	5:41A		5:50A	5:56A	6:00A	6:06A	\n" +
		"5:19A		5:28A	5:31A	5:36A	5:42A	5:47A		5:54A	5:59A	6:08A	6:09A		6:18A	6:24A	6:28A	6:34A	\n" +
		"		5:39A		5:46A	5:51A	5:57A	6:02A		6:09A	6:14A	6:24A			6:33A	6:39A	6:43A	6:49A	\n"
		;

	public void testParserFile() throws IOException {
		final Parser p = new Parser();
		p.parse(new StringReader(TEST_TDL));
		assertTrue("parcer is not valid", p.isValid());
		assertEquals("number of stations", p.getStationList().length, 17);
		assertEquals("number of schedules", p.getTimeTable()[0].length, 2);
		assertEquals("number of schedules", p.getTimeTable()[1].length, 2);
		assertEquals("number of schedules", p.getTimeTable()[2].length, 4);
		assertEquals("number of schedules", p.getTimeTable()[3].length, 3);
		assertNotNull("not null", p.getTimeTable()[0][1]);
		assertEquals("last time", p.getTimeTable()[16][0], "05:30A");
	}

	public void testParserNet() throws IOException {
		final File file = File.createTempFile("@@@", ".tmp", getContext().getCacheDir());
		final File net = Net.getFileFromNet("http://www.vta.org/schedules/tdl/SC_330NO_WK.tdl", file.getAbsolutePath());

		final Parser p = new Parser();
		p.parse(net);
		assertTrue("parcer is not valid", p.isValid());
	}
}
